package com.dragoncombin.homework.Module04.Problem01;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StudentList {

    List<Student> students = new ArrayList<>();
    private static StudentList stdListObj = null;

    static {
        try {
            stdListObj = new StudentList();
        } catch (IdSetingIllegalException | AgeException e) {
            e.printStackTrace();
        }
    }

    private StudentList() throws IdSetingIllegalException, AgeException {
        // 无参构造默认给成员变量students初始化
        BufferedReader br = null;
        try {
            // 从文件"studentInfo.txt"中读取学生信息,分别创建Student对象，存入List集合中
            br = new BufferedReader(new FileReader("f:/学习记录/homework/my-java-home-work/code/module04/studentInfo.txt"));
            String str = br.readLine();
            // 忽略读取到的第一行，因为其为标题行
            System.out.println("正在读取学生信息...");
            Thread.sleep(1000);
            while (null != (str = br.readLine())) {
                String[] strArray = str.split("\t");

                // 创建Student对象，并将其放入List集合中
                Student st = new Student(Integer.parseInt(strArray[0]), strArray[1], Integer.parseInt(strArray[2]));
                students.add(st);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public List<Student> getStudents() {
        return students;
    }

    public static StudentList getInstance() {
        return stdListObj;
    }

}
